package com.bee.gateway.service.impl;

import com.bee.commons.cache.CacheService;
import com.bee.gateway.common.constants.GatewayRedisConst;
import com.bee.gateway.common.dto.MsgContentDTO;
import com.bee.gateway.common.enums.RoutesMsgTypeEnum;
import com.bee.gateway.mapper.IgnoreAuthMapper;
import com.bee.gateway.common.domain.IgnoreAuthDO;
import com.bee.gateway.common.enums.StatusEnum;
import com.bee.gateway.common.vo.IgnoreAuthQueryVO;
import com.bee.gateway.service.IgnoreAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 忽略鉴权配置
 * @author Damin.yang
 * @date 2019/07/27 23:27
 */
@Service
@Slf4j
public class IgnoreAuthServiceImpl implements IgnoreAuthService {

    @Autowired
    private IgnoreAuthMapper ignoreAuthMapper;
    @Autowired
    private CacheService cacheService;

    @Override
    public int insert(IgnoreAuthDO ignoreAuthDO) {
        ignoreAuthDO.setStatus(StatusEnum.Y.name());
        int result = ignoreAuthMapper.insert(ignoreAuthDO);
        if (result == 1) {
            // 添加到缓存并通知网关
            cacheService.set(GatewayRedisConst.GATEWAY_IGNORE_AUTH_PREFIX + ignoreAuthDO.getUri(), ignoreAuthDO.getUri());
            MsgContentDTO dto = new MsgContentDTO(RoutesMsgTypeEnum.SAVE.name(), ignoreAuthDO.getUri());
            cacheService.publish(GatewayRedisConst.GATEWAY_IGNORE_AUTH_CHANNEL, dto);
        }

        return result;
    }

    @Override
    public IgnoreAuthDO get(long id) {
        return ignoreAuthMapper.get(id);
    }

    @Override
    public List<IgnoreAuthDO> query(IgnoreAuthQueryVO queryVO) {
        return ignoreAuthMapper.query(queryVO);
    }

    @Override
    public void update(IgnoreAuthDO ignoreAuthDO) {
        IgnoreAuthDO oldIgnoreAuthDO = get(ignoreAuthDO.getId());
        ignoreAuthMapper.update(ignoreAuthDO);

        if (!ignoreAuthDO.getUri().equals(oldIgnoreAuthDO.getUri())) {
            // 添加到缓存并通知网关
            cacheService.set(GatewayRedisConst.GATEWAY_IGNORE_AUTH_PREFIX + ignoreAuthDO.getUri(),
                    oldIgnoreAuthDO.getUri() + "," + ignoreAuthDO.getUri());
            MsgContentDTO dto = new MsgContentDTO(RoutesMsgTypeEnum.SAVE.name(), ignoreAuthDO.getUri());
            cacheService.publish(GatewayRedisConst.GATEWAY_IGNORE_AUTH_CHANNEL, dto);
        }
    }

    @Override
    public void delete(long id) {
        IgnoreAuthDO ignoreAuthDO = get(id);
        ignoreAuthMapper.delete(id);
        // 通知网关
        cacheService.set(GatewayRedisConst.GATEWAY_IGNORE_AUTH_PREFIX + ignoreAuthDO.getUri(), ignoreAuthDO.getUri());
        MsgContentDTO dto = new MsgContentDTO(RoutesMsgTypeEnum.DELETE.name(), ignoreAuthDO.getUri());
        cacheService.publish(GatewayRedisConst.GATEWAY_IGNORE_AUTH_CHANNEL, dto);
    }

    @Override
    public void overload() {
        // 重新load所有到缓存
        cacheAllIgnoreAuth();
        // 通知网关
        MsgContentDTO dto = new MsgContentDTO(RoutesMsgTypeEnum.OVERLOAD.name());
        cacheService.publish(GatewayRedisConst.GATEWAY_IGNORE_AUTH_CHANNEL, dto);
    }

    @Override
    public void cacheAllIgnoreAuth() {
        IgnoreAuthQueryVO queryVO = new IgnoreAuthQueryVO();
        queryVO.setStatus(StatusEnum.Y.name());
        List<IgnoreAuthDO> dos = query(queryVO);
        dos.forEach(ignoreAuthDO ->
                cacheService.set(GatewayRedisConst.GATEWAY_IGNORE_AUTH_PREFIX + ignoreAuthDO.getUri(), ignoreAuthDO.getUri())
        );
    }
}
